/**
 * 
 */
$(document).ready(
	function ()
	{
		$('.sortable').each(
			function (i)
			{
				var table = $(this),
				oTable = table.dataTable({
					aoColumns: [
						{ bSortable: false },
						{sType: 'string'},
						{bType: 'string'},
						{ bSortable: false }
					],
					
					sDom: '<"block-controls"<"controls-buttons"p>>rti<"block-footer clearfix"lf>',
	
					fnDrawCallback: function()
					{
						this.parent().applyTemplateSetup();
					},
	
					fnInitComplete: function()
					{
						this.parent().applyTemplateSetup();
					}
				});
	
				table.find('thead .sort-up').click(
					function(event)
					{
						event.preventDefault();
	
						var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));
	
						oTable.fnSort([[columnIndex, 'asc']]);
	
						return false;
					}
				);
	
				table.find('thead .sort-down').click(
					function(event)
					{
						event.preventDefault();
	
						var column = $(this).closest('th'),
						columnIndex = column.parent().children().index(column.get(0));
	
						oTable.fnSort([[columnIndex, 'desc']]);
	
						return false;
					}
				);
			}
		);
		
		$('#novo').click(
			function ()
			{
				dialog( baseUrl + '/admin/local/form' );
			}
		);
		
	    $.fn.updateTabs.enabledHash = false;
		
	}
);


/**
 * 
 * @param url
 */
function dialog ( url )
{
	var option = {};
	
	option.title 	= 'Local';
	option.url 		= url;
        option.width        =  520;
	option.complete	= function() { 
		$.modal.current.centerModal();
		resizeTab();
	};
    option.buttons 	= { 'Salvar': function () { $('#form-local').submit(); } };

	showDialog( option );
}

/**
 * 
 * @param form
 * @returns
 */
function save ( form )
{
	$('#form-material').removeBlockMessages().blockMessage( 'Aguarde...', {type: 'warning'} );
	
	return true;
}

/**
 * 
 */
function addFile ()
{
	if ( $('#files').find('tr').length == 4 ) {
		alert('Você pode enviar no maximo 5 arquivos de uma vez');
		return;
	}

	var tr = $('<tr>');
	var td = $('<td>');
	
	var span 		= $('<span class="input-type-text container-upload">');
	var inputText 	= $('<input type="text" class="desc-file">');
	var img 		= $('<img src="' + baseUrl + '/public/images/admin/icons/fugue/magnifier.png">');
	
	var inputFile 	= $('<input class="input-file" type="file" name="imagem[]" onChange="showFileSelected(this)">');
	
	span.append( inputText );
	span.append( img );
	
	td.append( span );
	td.append( inputFile );
	
	tr.append( td );
	
	var a = $('<a href="#" onClick="removeFile(this);">');
	a.append( $('<img src="' + baseUrl + '/public/images/admin/icons/fugue/cross-circle.png">') );
	
	tr.append( $('<td>').append(a) );
	
	$('#files').append( tr );
}

/**
 * 
 * @param elem
 */
function removeFile ( elem )
{
	if ( confirm('Deseja realmente remover?') )
		$(elem).parent().parent().remove();
}

/**
 * 
 * @param iframe
 * @returns {Boolean}
 */
function callBackUpload ( iframe )
{
	var div = $(iframe).contents().find('div');
    
    if ( div.length ) {
    	var result = eval('(' + div.eq(0).html() + ')' );
    	
		$(result.description).each(
		    function ( index, value )
		    {
		    	$('#form-local').removeBlockMessages().blockMessage( value.message, {type: value.level} );
		    }
	    );
		
		if ( result.status ) {
			
			history.go(0);
			
		} else {
			
		    if ( result.errors ) {
		    	showErrorsForm( result.errors );
		    	errorUpload('imagem');
		    }
		    
		}
		
    }
    
    return true;
}

function selectImage( elem, id )
{
	console.log( id );
}
